package com.weilus.clients;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.weilus.refresh.CilentsConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.Duration;
import java.util.Map;

@Configuration
public class FluxService {

    @Autowired
    WebClient.Builder builder;

    @Autowired
    CilentsConfig config;

    @HystrixCommand(fallbackMethod = "sayHelloFallback")
    public String sayHello(Map map){
        return builder.build()
                .post()
                .uri(config.getSayHello())
                .bodyValue(map)
                .retrieve()
                .bodyToMono(String.class)
                .block(Duration.ofSeconds(30));
    }

    @HystrixCommand(fallbackMethod = "hiManFallback")
    public String hiMan(Map<String, String> map){
        return builder.build()
                .post()
                .uri(config.getHiMan())
                .bodyValue(map)
                .retrieve()
                .bodyToMono(String.class)
                .block(Duration.ofSeconds(30));
    }

    public String sayHelloFallback(Map map) {
        return "flux-call-sayHelloFallback: 你好 "+config.getName();
    }

    public String hiManFallback(Map<String, String> map) {
        return "flux-call-hiManFallback: Hi "+config.getName();
    }

}
